package servlet;

import bean.Result;
import com.google.gson.Gson;
import dao.QuerylistDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/querylist.s")
public class QueryListServlet extends HttpServlet {

    QuerylistDao dao=new QuerylistDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final HttpSession session = req.getSession();
        final Integer uid = (Integer) session.getAttribute("userid");
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        System.out.println(currentPage);
        System.out.println(pageSize);
        int page = Integer.valueOf(currentPage);
        int size = Integer.valueOf(pageSize);

        System.out.println("============"+page);
        Map<String,Object> map=new HashMap<>();
        Result result;
        try{
            map=  dao.list( uid,size * (page - 1), size);
            System.out.println(map);
            result =new Result(1,"查询成功",map);
        }catch (Exception e){
            result=new Result(0,e.getMessage(),null);
        }

        final String json = new Gson().toJson(result);
        resp.getWriter().append(json);
    }
}
